package com.whsxt.controller;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-15 10:53
 * @Description:
 */
@RestController
@RequestMapping("/admin/file")
@Api(tags = "文件上传接口")
@Slf4j
public class FileController {


    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Value("${resources.url}")
    private String host;


    @PostMapping("/upload/element")
    @ApiOperation(value = "文件上传")
    public ResponseEntity<String> uploadFile(@RequestParam MultipartFile file) {
        if (ObjectUtils.isEmpty(file)) {
            return ResponseEntity.ok().build();
        }

        //返回文件路径  fullPath没有ip
        String fullPath = null;
        try {
            fullPath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), "jpg", null).getFullPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("上传成功路径：{}", host + "/" + fullPath);
        return ResponseEntity.ok(host + "/" + fullPath);
    }

}
